package com.android.hedgehog;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.scoreloop.client.android.core.controller.RankingController;
import com.scoreloop.client.android.core.controller.RequestController;
import com.scoreloop.client.android.core.controller.RequestControllerObserver;
import com.scoreloop.client.android.core.controller.ScoreController;
import com.scoreloop.client.android.core.controller.UserController;
import com.scoreloop.client.android.core.model.Score;
import com.scoreloop.client.android.core.model.SearchList;
import com.scoreloop.client.android.core.model.Session;
import com.scoreloop.client.android.core.model.User;

public class ScoreManager implements RequestControllerObserver {

	// private ScoreInterface game;

	// Create the controller instances
	final ScoreController scoreController = new ScoreController(this);
	final RankingController globalRankingController = new RankingController(this);
	UserController myUserController = new UserController(this);

	public ScoreManager() {
		globalRankingController.setSearchList(SearchList.getGlobalScoreSearchList());
	}

	@Override
	public void requestControllerDidFail(RequestController arg0, Exception arg1) {

	}

	@Override
	public void requestControllerDidReceiveResponse(RequestController controller) {

		if (controller == myUserController) {
			// To add user context
		}

		if (controller == scoreController) {
			// if score just submitted, fetch global rank
			globalRankingController.loadRankingForUserInGameMode(Session.getCurrentSession().getUser(), 0);

		} else if (controller == globalRankingController) {
			// set the global rank in the game
			Settings.globalRank = (globalRankingController.getRanking().getRank());
		}

	}

	public void uploadScore(int scoreToUpload) { // now upload IQ

		User user = Session.getCurrentSession().getUser();
		Map<String, Object> userContext = new HashMap<String, Object>();
		userContext.put("username", Settings.userName);
		Integer age = new Integer(Settings.age);
		userContext.put("age", age);
		user.setContext(userContext);
		myUserController.setUser(user);
		myUserController.submitUserContext();
		
		Date date= new Date();
		user.setDateOfBirth(date);
		myUserController.setUser(user);
		myUserController.submitUser();

		final Score score = new Score((double) scoreToUpload, null);
		score.setMode(1);
		scoreController.submitScore(score);

	}

}
